#ifndef __LED_H__
#define __LED_H__

#include <stdint.h>
#include <stdbool.h>

/**
 * @brief LED点亮模式
 *
 */
typedef enum
{
    /** LED 高电平点亮 */
    led_off = 0x1,          /* 常亮 */
    led_on = 0x0,           /* 常灭 */
    led_singleflash = 0x41, /* 单闪 亮200ms灭1s*/
    led_doubleflash = 0x42, /* 双闪 亮200ms灭200ms亮200ms灭800ms*/
    /** 50%占空比 无所谓高低点亮 */
    led_blinking = 0x40,   /* 闪烁 周期200ms翻转*/
    led_flickering = 0x60, /* 快闪 周期50ms*/
    /** LED 低电平点亮 */
    led_inverton = 0x80,          /* 取反常亮 */
    led_invertoff = 0x81,         /* 取反常灭 */
    led_invertsingleflash = 0xC1, /* 取反单闪 亮200ms灭1s*/
    led_invertdoubleflash = 0xC2, /* 取反双闪 亮200ms灭200ms亮200ms灭800ms*/
} ledctrlmode_t;

/*---------------------------------------------
      Codes for LED
      bit 7: invert flag
      bit 6: toggle    (if toggle == 1 and number of flashes == 0 => infinite toggle)
      bit 5: fast toggle flag (50ms cycle)
      bit 4-0: number of flashes
-----------------------------------------------*/
typedef union LedCmd
{
    uint8_t byte; /* 控制指令 */
    struct
    {
        uint8_t num_of_flash : 5; /* 闪烁次数 */
        uint8_t fast_toggle : 1;  /*  */
        uint8_t toggle : 1;       /*  */
        uint8_t invert : 1;       /* 取反标志 0 HIGH亮  1 HIGH灭 */
    } bit;
} ledcmd_t;

/**
 * @brief led模块控制数据结构体
 *
 */
typedef struct LedCtrl
{
    ledcmd_t cmd;
    ledcmd_t cmd_prev;
    uint8_t cnt;
    uint8_t state:1;
    uint8_t state_last:1;
    uint8_t state_output:1;
    uint8_t reserved:5;
    uint16_t ms;
    void (*wrgpio)(bool);
} ledctrl_t;

int32_t led_setmode(ledctrl_t*p, ledctrlmode_t m);
void led_control(ledctrl_t*p);

/*demo

#include "led.h"
void run1_wrgpio(bool s)
{
    BIT_BAND(&GPIOG->ODR, 7) = s;
}
ledctrl_t run1 = {
    .cmd.byte = led_blinking,
    .wrgpio = run1_wrgpio,
};
void HAL_SYSTICK_Callback(void)
{
    led_control(&run1);
}

*/
#endif
